#pragma once
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <boost/array.hpp>

#include "ParseHttpRequest.h"

using boost::asio::ip::tcp;


class TcpConnection : public boost::enable_shared_from_this<TcpConnection>
{
public:
	TcpConnection( boost::asio::io_service& io_service);
	~TcpConnection(void);

	typedef boost::shared_ptr<TcpConnection> pointer;
	static pointer create(boost::asio::io_service& io_service)
	{
		return pointer(new TcpConnection(io_service));
	}

	tcp::socket& socket();
	void start();

private:
	void client_handle_read_headers(const boost::system::error_code& err);

	void handle_resolve(const boost::system::error_code& err,
      tcp::resolver::iterator endpoint_iterator);
	void handle_connect(const boost::system::error_code& err);
	void handle_write_request(const boost::system::error_code& err);
	void handle_read_status_line(const boost::system::error_code& err);
	void handle_read_headers(const boost::system::error_code& err);
	void TcpConnection::handle_read_content(const boost::system::error_code& err);

	tcp::socket socket_;
	tcp::socket remote_socket_;

	tcp::resolver resolver_;

	std::string message_;
	int i;

	//stream of client
	boost::asio::streambuf request_;
	boost::asio::streambuf response_;

	//stream of web server
	boost::asio::streambuf remote_request_;
	boost::asio::streambuf remote_response_;

	/// The incoming request.
	request request_data_;

	/// The parser for the incoming request.
	ParceHttpRequest request_parser_;

};